package com.sky.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.IDishService;
import com.sky.service.ISetmealService;
import com.sky.service.IShoppingCartService;
import com.sky.utils.ThreadLocalUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService iShoppingCartService;

    @Autowired
    private ISetmealService iSetmealService;
    @Autowired
    private IDishService iDishService;

    private final Integer numberX =1;

    @GetMapping("/list")
    private Result list(){
        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,ThreadLocalUtil.getValue());
        List<ShoppingCart> list = iShoppingCartService.list(lqw);
        return Result.success(list);
    }

    @PostMapping("/add")
    private Result add(@RequestBody ShoppingCartDTO dto){
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = (Long)ThreadLocalUtil.getValue();
        LambdaQueryWrapper<ShoppingCart>lqw =new LambdaQueryWrapper<>();
        if (dto.getSetmealId()!=null){
            Setmeal setmeal = iSetmealService.getById(dto.getSetmealId());
            BeanUtils.copyProperties(setmeal,shoppingCart);
            shoppingCart.setSetmealId(setmeal.getId());
            shoppingCart.setAmount(setmeal.getPrice());
            lqw.eq(ShoppingCart::getSetmealId,dto.getSetmealId());

            //todo:菜品口味对比
        }else if (dto.getDishId()!=null){
            //没有口味是因为穿的不是vo么
            Dish dish = iDishService.getById(dto.getDishId());
            BeanUtils.copyProperties(dish,shoppingCart);
            shoppingCart.setDishId(dish.getId());
            shoppingCart.setDishFlavor(dto.getDishFlavor());
            shoppingCart.setAmount(dish.getPrice());
            lqw.eq(ShoppingCart::getDishId,dto.getDishId());
        }
        shoppingCart.setId(null);
        shoppingCart.setUserId(userId);
        lqw.eq(ShoppingCart::getUserId,userId);
        //添加之前应该判断一下，是否存在，则就加一即可
        ShoppingCart one = iShoppingCartService.getOne(lqw);
        if (one==null){//如果不存在  -- 添加
            shoppingCart.setNumber(numberX);
            iShoppingCartService.save(shoppingCart);
        }else {         //-- 修改
            shoppingCart.setNumber(one.getNumber()+numberX);
            iShoppingCartService.update(shoppingCart,lqw);
        }
        return Result.success();
    }

    @PostMapping("/sub")
    public Result sub (@RequestBody ShoppingCartDTO shoppingCartDTO){
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        //dish
            LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();
            lqw.eq(dishId!=null,ShoppingCart::getDishId,dishId);
            lqw.eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);
            lqw.eq(ShoppingCart::getUserId,ThreadLocalUtil.getValue());
        //先查看购物车的数量，
        ShoppingCart one = iShoppingCartService.getOne(lqw);
        //大于1，则修改个数
        if (one.getNumber()>1){
            LambdaUpdateWrapper<ShoppingCart> luw =new LambdaUpdateWrapper<>();
            luw.set(ShoppingCart::getNumber,one.getNumber()-numberX);
            luw.eq(ShoppingCart::getId,one.getId());
            iShoppingCartService.update(luw);
        }else {//为1，则直接删除
            iShoppingCartService.removeById(one);
        }
        return Result.success();
    }

    @DeleteMapping("/clean")
    public Result clear(){
        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,ThreadLocalUtil.getValue());
        iShoppingCartService.remove(lqw);
        return Result.success();
    }
}
